package com.android.personal.kotlin.activity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.personal.kotlin.R;
import com.android.personal.kotlin.client.ApiFactory;
import com.android.personal.kotlin.client.ApiManager;
import com.android.personal.kotlin.response.AllCpResponse;
import com.android.personal.kotlin.utils.CopyAssetsFiles;

import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by zhangwentao on 2016/12/29.
 */

public class RetrofitTestActivity extends BaseAppCompatActivity implements View.OnClickListener {

    TextView mResult;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.retrofit_test_activity);
        initViews();
    }

    private void initViews() {
        getToolbarTitle().setText("测试Retrofit2.0 请求");
        getSubTitle().setText("列表按钮");

        mResult = (TextView) findViewById(R.id.result);

        findViewById(R.id.get_no_parama).setOnClickListener(this);
        findViewById(R.id.get_parama).setOnClickListener(this);
        findViewById(R.id.get_observable).setOnClickListener(this);
        findViewById(R.id.toolbar_subtitle).setOnClickListener(this);
        findViewById(R.id.get_contacts).setOnClickListener(this);
        findViewById(R.id.copy_file).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_subtitle:
                startActivity(new Intent(this, ListButtonActivity.class));
                break;
            case R.id.get_no_parama:
                ApiFactory.getInstance().getCp(new Callback<AllCpResponse>() {
                    @Override
                    public void onResponse(Call<AllCpResponse> call, Response<AllCpResponse> response) {
                        AllCpResponse allCpResponse = response.body();
                        mResult.setText(allCpResponse.toString());
                    }

                    @Override
                    public void onFailure(Call<AllCpResponse> call, Throwable t) {

                    }
                });
                break;
            case R.id.get_parama:
                Map<String, Object> map = new HashMap<>();
                map.put("category", "1");
                ApiFactory.getInstance().getInspectReservationString(map, new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        mResult.setText(response.body());
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                    }
                });
                break;
            case R.id.get_observable:
                /**
                 * .subscribeOn(Schedulers.io())指定Observable的工作, 在我们的例子中Observable的工作即发送请求, 在io线程做, 指定了被观察者的处理线程;
                 * .observeOn(AndroidSchedulers.mainThread())指定最后onNext()回调在主线程, 即指定了通知后续观察者的线程.
                 */
                ApiManager.getRetrofitService().getObservableCp()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<String>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(String allCpResponse) {
                                mResult.setText(allCpResponse);
                            }
                        });
                break;
            case R.id.get_contacts:
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "有权限", Toast.LENGTH_SHORT).show();
                } else {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS},
                            0);
                }
                break;
            case R.id.copy_file:
                String[] names = {"BeijingSubway.db", "StationDetail.db", "subway.db", "subway_test.db"};
                for (String name : names) {
                    CopyAssetsFiles.copyAssetsFiles(this, getDatabasePath(name).getParent(), name);
                }
                break;
        }
    }
}
